package com.yuntsg.ruiijn.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.yuntsg.ruiijn.database.entity.WosBasic;
import com.yuntsg.ruiijn.database.mapper.WosBasicMapper;
import com.yuntsg.ruiijn.database_entity.Pubmed;
import common.util.StringUtil;
import com.yuntsg.nnsfcp.common.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static common.util.PubmedUtil.getPubmedInfo;

@Slf4j
@SuppressWarnings("all")
@Service
public class UploadCnService {

    @Resource
    private UserUtil userUtil;
    @Autowired
    private WosBasicMapper wosBasicMapper;

    public static void main(String[] args) {
        String s = HttpUtil.get(StrUtil.format("https://pm.yuntsg.com/utils/getpuinfotong?type=1&q={}", "26192428"));
        System.out.println(s);
    }

    /**
     * 知网数据
     *
     * @param filePath
     * @param searchPlanCn
     * @param gid
     */
    public void cnki(String filePath, String searchPlanCn, Integer gid) {
        List<String> lines = FileUtil.readLines(filePath, "utf8");
        int count = 0;
        WosBasic wosBasic = null;
        for (String line : lines) {
            if (line.startsWith("RT")) {
                wosBasic = new WosBasic();
                //设置为知网数据
                wosBasic.setSoType(5);
                wosBasic.setUid(1);
                wosBasic.setGid(gid);
            } else if (line.startsWith("A1")) {
                // wosBasic.setCC1(line.replace("A1", "").replace(" ", ""));
            } else if (line.startsWith("AD")) {
                try {
                    String ad = line.replace("AD", "");
                    String[] split = ad.split(";");
                    wosBasic.setCC1(split[0]);
                    StringBuilder cRp = new StringBuilder();
                    for (int i = 1; i < split.length; i++) {
                        cRp.append(split[i]).append("; ");
                    }
                    wosBasic.setCRp(cRp.toString());
                } catch (Exception e) {

                }
            } else if (line.startsWith("T1")) {
                wosBasic.setCTi(line.replace("T1", "").trim());
            } else if (line.startsWith("JF")) {
                wosBasic.setCSo(line.replace("JF", "").trim());
            } else if (line.startsWith("YR")) {
                wosBasic.setCPy(line.replace("YR", "").trim());
            } else if (line.startsWith("IS")) {
                wosBasic.setCIs(line.replace("IS", "").trim());
            } else if (line.startsWith("VO")) {
                wosBasic.setCVl(line.replace("VO", "").trim());
            } else if (line.startsWith("OP")) {
                // wosBasic.setPage(line.replace("OP", "").trim());
            } else if (line.startsWith("K1")) {
                //wosBasic.setck(line.replace("K1", "").trim());
            } else if (line.startsWith("AB")) {
                wosBasic.setCAb(line.replace("AB", "").trim());
            } else if (line.startsWith("SN")) {
                wosBasic.setCSn(line.replace("SN", "").trim());
            } else if (line.startsWith("LK")) {
                wosBasic.setCnkiUrl(line.replace("LK", "").trim());
            } else if (line.startsWith("DO")) {
                wosBasic.setCDi(line.replace("DO", "").trim());
            } else if (StrUtil.isBlank(line)) {
                if (wosBasic != null) {
                    try {
                        boolean coFlag = false;
                        boolean rpFlag = false;
                        boolean auFlag = false;
                        boolean cyFlag = false;
                        /**
                         * 判断是否是第一作者 通讯作者
                         */
                        String cc1 = wosBasic.getCC1();
                        if (cc1.contains(searchPlanCn)) {
                            coFlag = true;
                        }
                        String cRp = wosBasic.getCRp();
                        String[] split = cRp.split("; ");
                        for (String s : split) {
                            if (s.contains(searchPlanCn)) {
                                rpFlag = true;
                            }
                        }
                        if (coFlag) {
                            wosBasic.setCoFlag(1);
                        } else {
                            wosBasic.setCoFlag(0);
                        }
                        if (rpFlag) {
                            wosBasic.setRpFlag(1);
                        } else {
                            wosBasic.setRpFlag(0);
                        }
                        if (coFlag || rpFlag) {
                            wosBasic.setAuFlag(1);
                        } else {
                            wosBasic.setAuFlag(0);
                        }
                    } catch (Exception e) {

                    }
                    count++;
                    wosBasicMapper.insert(wosBasic);
                    wosBasic = null;
                }
            }
        }
        System.out.println("本次成功上传文献:" + count + "条");
    }

    /**
     * 万方数据
     *
     * @param filePath
     * @param searchPlanCn
     * @param gid
     */
    public void wf(String filePath, String searchPlanCn, Integer gid) {
        List<String> lines = FileUtil.readLines(filePath, "utf8");
        int count = 0;
        WosBasic wosBasic = null;
        for (String line : lines) {
            if (line.startsWith("RT")) {
                wosBasic = new WosBasic();
                //设置为万方数据
                wosBasic.setSoType(6);
                wosBasic.setUid(1);
                wosBasic.setGid(gid);
            } else if (line.startsWith("A1")) {
                //wosBasic.setCnauthor(line.replace("A1", "").replace(" ", ""));
            } else if (line.startsWith("AD")) {
                try {
                    String ad = line.replace("AD", "");
                    String[] split = ad.split(";");
                    wosBasic.setCC1(split[0]);
                    StringBuilder cRp = new StringBuilder();
                    for (int i = 1; i < split.length; i++) {
                        cRp.append(split[i]).append("; ");
                    }
                    wosBasic.setCRp(cRp.toString());
                } catch (Exception e) {

                }
            } else if (line.startsWith("T1")) {
                wosBasic.setCTi(line.replace("T1", "").trim());
            } else if (line.startsWith("T2")) {
                //wosBasic.setEntitle(line.replace("T2", "").trim());
            } else if (line.startsWith("JF")) {
//                if (StrUtil.isNotBlank(wosBasic.getCnjournal())) {
//                    wosBasic.setEnjournal(line.replace("JF", "").trim());
//                } else {
//                    wosBasic.setCnjournal(line.replace("JF", "").trim());
//                }
            } else if (line.startsWith("YR")) {
                wosBasic.setCPy(line.replace("YR", "").trim());
            } else if (line.startsWith("IS")) {
                wosBasic.setCIs(line.replace("IS", "").trim());
            } else if (line.startsWith("VO")) {
                wosBasic.setCVl(line.replace("VO", "").trim());
            } else if (line.startsWith("OP")) {
                wosBasic.setCPg(line.replace("OP", "").trim());
            } else if (line.startsWith("K1")) {
                //wosBasic.setCnkeyword(line.replace("K1", "").trim());
            } else if (line.startsWith("AB")) {
                wosBasic.setCAb(line.replace("AB", "").trim());
            } else if (line.startsWith("SN")) {
                wosBasic.setCSn(line.replace("SN", "").trim());
            } else if (line.startsWith("LK")) {
                wosBasic.setWfUrl(line.replace("LK", "").trim());
            } else if (line.startsWith("DO")) {
                wosBasic.setCDi(line.replace("DO", "").trim());
            } else if (StrUtil.isBlank(line)) {
                if (wosBasic != null) {
                    try {
                        boolean coFlag = false;
                        boolean rpFlag = false;
                        boolean auFlag = false;
                        boolean cyFlag = false;
                        /**
                         * 判断是否是第一作者 通讯作者
                         */
                        String cc1 = wosBasic.getCC1();
                        if (cc1.contains(searchPlanCn)) {
                            coFlag = true;
                        }
                        String cRp = wosBasic.getCRp();
                        String[] split = cRp.split("; ");
                        for (String s : split) {
                            if (s.contains(searchPlanCn)) {
                                rpFlag = true;
                            }
                        }
                        if (coFlag) {
                            wosBasic.setCoFlag(1);
                        } else {
                            wosBasic.setCoFlag(0);
                        }
                        if (rpFlag) {
                            wosBasic.setRpFlag(1);
                        } else {
                            wosBasic.setRpFlag(0);
                        }
                        if (coFlag || rpFlag) {
                            wosBasic.setAuFlag(1);
                        } else {
                            wosBasic.setAuFlag(0);
                        }
                    } catch (Exception e) {

                    }
                    count++;
                    wosBasicMapper.insert(wosBasic);
                    wosBasic = null;
                }
            }

        }
        System.out.println("本次成功上传文献:" + count + "条");
    }

    /**
     * 维普数据
     *
     * @param filePath
     * @param searchPlanCn
     * @param gid
     */
    public void cqvip(String filePath, String searchPlanCn, Integer gid) {
        List<String> lines = FileUtil.readLines(filePath, "utf8");
        int count = 0;
        WosBasic wosBasic = null;
        for (String line : lines) {
            if (line.startsWith("RT")) {
                wosBasic = new WosBasic();
                //设置为维普数据
                wosBasic.setSoType(7);
                wosBasic.setUid(1);
                wosBasic.setGid(gid);
            } else if (line.startsWith("A1")) {
                //wosBasic.setCnau(line.replace("A1", "").replace(" ", ""));
            } else if (line.startsWith("AD")) {
                try {
                    String ad = line.replace("AD", "");
                    String[] split = ad.split(";");
                    wosBasic.setCC1(split[0]);
                    StringBuilder cRp = new StringBuilder();
                    for (int i = 1; i < split.length; i++) {
                        cRp.append(split[i]).append("; ");
                    }
                    wosBasic.setCRp(cRp.toString());
                } catch (Exception e) {

                }
            } else if (line.startsWith("T1")) {
                wosBasic.setCTi(line.replace("T1", "").trim());
            } else if (line.startsWith("T2")) {
                // wosBasic.setEntitle(line.replace("T2", "").trim());
            } else if (line.startsWith("JF")) {
                //wosBasic.setCnfrom(line.replace("JF", "").trim());
            } else if (line.startsWith("YR")) {
                wosBasic.setCPy(line.replace("YR", "").trim());
            } else if (line.startsWith("IS")) {
                wosBasic.setCIs(line.replace("IS", "").trim());
            } else if (line.startsWith("VO")) {
                wosBasic.setCVl(line.replace("VO", "").trim());
            } else if (line.startsWith("OP")) {
//                bean.setPage(line.replace("OP", "").trim());
            } else if (line.startsWith("K1")) {
                // wosBasic.setCnkeyword(line.replace("K1", "").trim());
            } else if (line.startsWith("AB")) {
                wosBasic.setCAb(line.replace("AB", "").trim());
            } else if (line.startsWith("SN")) {
                wosBasic.setCSn(line.replace("SN", "").trim());
            } else if (line.startsWith("LK")) {
                wosBasic.setWpUrl(line.replace("LK", "").trim());
            } else if (line.startsWith("DO")) {
                wosBasic.setCDi(line.replace("DO", "").trim());
            } else if (StrUtil.isBlank(line)) {
                if (wosBasic != null) {
                    try {
                        boolean coFlag = false;
                        boolean rpFlag = false;
                        boolean auFlag = false;
                        boolean cyFlag = false;
                        /**
                         * 判断是否是第一作者 通讯作者
                         */
                        String cc1 = wosBasic.getCC1();
                        if (cc1.contains(searchPlanCn)) {
                            coFlag = true;
                        }
                        String cRp = wosBasic.getCRp();
                        String[] split = cRp.split("; ");
                        for (String s : split) {
                            if (s.contains(searchPlanCn)) {
                                rpFlag = true;
                            }
                        }
                        if (coFlag) {
                            wosBasic.setCoFlag(1);
                        } else {
                            wosBasic.setCoFlag(0);
                        }
                        if (rpFlag) {
                            wosBasic.setRpFlag(1);
                        } else {
                            wosBasic.setRpFlag(0);
                        }
                        if (coFlag || rpFlag) {
                            wosBasic.setAuFlag(1);
                        } else {
                            wosBasic.setAuFlag(0);
                        }
                    } catch (Exception e) {

                    }
                    count++;
                    wosBasicMapper.insert(wosBasic);
                    wosBasic = null;
                }
            }

        }
        System.out.println("本次成功上传文献:" + count + "条");

    }

    public void pubmed(String filePath, String searchPlanCn, Integer gid) {

        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(filePath));
        List<List<Object>> read = reader.read();
        for (List<Object> objects : read) {
            for (Object object : objects) {
                Pubmed pubmedInfo = getPubmedInfo(object.toString());
                if (null != pubmedInfo && StringUtil.isNotNull(pubmedInfo.getPmid())) {
                    WosBasic wosBasic = new WosBasic();
                    //设置为维普数据
                    wosBasic.setSoType(4);
                    wosBasic.setUid(1);
                    wosBasic.setGid(gid);
                    wosBasic.setCC1(pubmedInfo.getAu());
                    wosBasic.setCoFlag(0);
                    wosBasic.setRpFlag(0);
                    wosBasic.setAuFlag(0);
                    wosBasic.setCyFlag(0);

                    String doi = pubmedInfo.getDoi();
                    wosBasic.setCDi(doi);
                    String pmid = pubmedInfo.getPmid();
                    wosBasic.setCPm(pmid);
                    String so = pubmedInfo.getSo();
                    wosBasic.setCSo(so);
                    String ti = pubmedInfo.getTi();
                    wosBasic.setCTi(ti);
                    String year = pubmedInfo.getYear();
                    wosBasic.setCPy(year);
                    wosBasicMapper.insert(wosBasic);
                    System.out.println("新增加的数据___" + pmid);
                }
            }
        }
    }
}
